<?php
/**
 * 门票
 *
 * @copyright  Copyright (c) 2015-2025 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com
 * @link       http://www.ncphp.com
 * @author	   NcPHP Team
 * @since      File available since Release v1.1
 */
defined('InNcPHP') or exit('Access Invalid!');

class ticketModel extends Model{

	public function __construct(){
		parent::__construct('ticket');
	}

    /**
     * 获取列表分页
     * @param array $condition
     * @param string $field
     * @param int $page
     * @param string $order
     * @return mixed
     */
    public function getTicketListPage($condition = array(), $field = '*', $page = null, $order = 'tk_add_time desc'){
        return $this->field($field)->where($condition)->page($page)->order($order)->select();
    }

    /**
     * 获取一条记录的详细信息
     * @param array $condition
     * @param string $field
     * @return mixed
     */
	public function getTicketInfo($condition = array(), $field = '*')
    {
        return $this->field($field)->where($condition)->find();
    }

    /**
     * 插入一条记录
     * @param array $params
     * @return mixed
     */
    public function addTicket($params)
    {
        return $this->insert($params);
    }

    /**
     * 更新记录
     * @param array $condition
     * @param array $params
     * @return mixed
     */
    public function updateTicket($condition, $params)
    {
        return $this->where($condition)->update($params);
    }

    /**
     * 删除记录
     * @param array $condition
     */
    public function deleteTicket($condition)
    {
        return $this->where($condition)->delete();
    }
    /**
     * 获取记录数组
     * @param array $condition
     * @param string $field
     * @param string $order
     * @return mixed
     */
    public function getTicketList($condition = array(), $field = '*', $order = 'tk_id desc'){
	    return $this->field($field)->where($condition)->order($order)->select();
    }

    /**
     * 增加销量
     * @param int $tk_id
     */
    public function addTicketSales($tk_id)
    {
        return $this->where(array('tk_id'=>$tk_id))->setInc('sales',1);
    }

    /**
     * 统计门票前六的城市
     */
    public function  gethotcity(){
      /*  $sql = "SELECT tk_city_id, COUNT(tk_city_id) AS COUNT FROM `nc_ticket` GROUP BY tk_city_id ORDER BY COUNT(tk_city_id) DESC LIMIT 6";*/

        $filed = "pw_ticket.tk_city_id,count(pw_ticket.tk_city_id) as total  ";
        $list = $this->where(array())->field($filed)->group("pw_ticket.tk_city_id")->order("count(pw_ticket.tk_city_id) desc")->limit(6)->select();
        return $list;

    }

    /**
     * 获取所有商品分类的城市ID（去重复）
     */
    public function getTicketCityId(){
        $city_list =  $this->where(array('tk_status'=>1))->field('tk_city_id')->group('tk_city_id')->select();
        /*if($city_list && is_array($city_list)){
            $arr = array();
            foreach($city_list as $key=>$value){
                $arr[$value['ht_city_id']][] =$value['ht_area_id'];
            }
            return $arr;

        }*/
        return $city_list;
    }

    /**
     * 获取所有商品分类的区ID（去重复）
     */
    public function getTicketAreaId(){
        $city_list =  $this->where(array('tk_status'=>1))->field('tk_area_id')->group('tk_area_id')->select();
        /*if($city_list && is_array($city_list)){
            $arr = array();
            foreach($city_list as $key=>$value){
                $arr[$value['ht_city_id']][] =$value['ht_area_id'];
            }
            return $arr;

        }*/
        return $city_list;
    }
    /**
     * 取得数量
     * @param array $condition
     */
    public function getTicketCount($condition) {
        return $this->table('ticket')->where($condition)->count();
    }


    public function get_status_count($condition){
        $field = "count(tk_status=1 or null ) as 'sale_count', count(tk_status=0 or null ) as 'un_sale_count' ";
        return $this->field($field)->where($condition)->find();
    }

}
?>